$(function() {
	// 页面加载初始化省份列表
	$(".province_option").remove();
	$.ajax({
		url : "province_getAllProvinces.action",
		type : "get",
		dataType : "json",
		success : function(data) {
			for (var i = 0; i < data.data.length; i++) {
				$("#s_province").append(
						"<option class='province_option' value='"
								+ data.data[i].id + "'>" + data.data[i].name
								+ "</option>")
			}
		}
	});

	// 省份改变，获取城市列表
	$("#s_province").on(
			"change",
			function() {
				var provinceId = $("#s_province :checked").val();
				$(".city_option").remove();
				$.ajax({
					url : "city_getCitysByProId.action",
					type : "get",
					data : {
						provinceId : provinceId
					},
					dataType : "json",
					success : function(data) {
						for (var i = 0; i < data.data.length; i++) {
							$("#s_city").append(
									"<option class='city_option' value='"
											+ data.data[i].id + "'>"
											+ data.data[i].name + "</option>")
						}
					}
				});
			});

	// 城市改变，获取高校列表
	$("#s_city").on(
			"change",
			function() {
				var cityId = $("#s_city :checked").val();
				$(".school_option").remove();
				$.ajax({
					url : "school_getSchoolsByCityId.action",
					type : "get",
					data : {
						cityId : cityId
					},
					dataType : "json",
					success : function(data) {
						for (var i = 0; i < data.data.length; i++) {
							$("#s_school").append(
									"<option class='school_option' value='"
											+ data.data[i].id + "'>"
											+ data.data[i].name + "</option>")
						}
					}
				});
			});

	$("#alterPwsBtn").click(
			function() {
				var oldPassword = $("#oldPassword").val();
				var newPassword = $("#newPassword").val();
				var passwordAgain = $("#passwordAgain").val();
				if (oldPassword == '') {
					$("#oldPassword").next().css("display", "");
					return;
				} else {
					$("#oldPassword").next().css("display", "none");
				}
				if (newPassword == '') {
					$("#newPassword").next().css("display", "");
					return;
				} else {
					$("#newPassword").next().css("display", "none");
				}
				if (passwordAgain == '') {
					$("#passwordAgain").next().css("display", "");
					return;
				} else {
					$("#passwordAgain").next().css("display", "none");
				}
				if (newPassword != passwordAgain) {
					$("#passwordAgain").next().next().css("display", "");
					return;
				} else {
					$("#passwordAgain").next().next().css("display", "none");
				}
				$.ajax({
					url : "member_checkPassword?member.id=" + memberId
							+ "&member.password=" + oldPassword,
					type : "get",
					success : function(data) {
						if ("true" == data.message) {
							$("#oldPassword").next().next().css("display",
									"none");
							$
									.ajax({
										url : "member_alterPws?memId="
												+ memberId + "&newPws="
												+ newPassword,
										type : "post",
										success : function(data2) {
											if ("true" == data2.message) {
												alert("修改成功！");
												$("#alterPswForm").find(
														"input[type=password]")
														.val("");
											} else {
												alert("修改失败！");
											}
										}
									})
						} else {
							$("#oldPassword").next().next().css("display", "");
						}
					}
				})
			});

})
var maxFileSize = 2 * 1024 * 1024;
function checkFile() {
	var file = $("#memPortraitFile").get(0).files[0];
	$("#uploadMessage").css("display", "none");
	if (file) {
		if (!file) {
			$("#uploadMessage").css("display", "");
			$("#uploadMessage").html("请选择一张图片。");
			return false;
		}
		if (file.size > maxFileSize) {
			$("#uploadMessage").css("display", "");
			$("#uploadMessage").html("图片不能超过2M");
			return false;
		}
		if (!/.jpg|.png/i.test($("#memPortraitFile").val())) {
			$("#uploadMessage").css("display", "");
			$("#uploadMessage").html("只支持jpg,png格式的图片");
			return false;
		}
		return true;
	}
}